﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Objects.CMT;
using System.Xml;
using System.Xml.Linq;
using CObject;
using Objects;
using System.Drawing;

public partial class Feedback : Objects.cPage
{
    int strAttendeeID;

    #region Feedbacks
    public enum Feedbacks
    {
        Company,
        Host,
        Tal,
        ITSSMP,
        ClientAttendees,
        CreatedEventAttendeeID,
        EventAttendeeID,
        Country,
        SubServiceLine,
        PrimObjectivesChallenges,
        PrimTaxIssues,
        OpportunityProjectIdentified,
        DescrOpportunityProject,
        Stage,
        PotentialTaxFees,
        ProbabilityOfWin,
        ExpectedTimeframe,
        NextSteps,
        ResponsibilityTakeForward,
        AdditionalInformation
    }
    #endregion

    #region Page_Load
    /// <summary>
    /// Page load event.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            CMTFeedback Feedback = new CMTFeedback();

            #region Event registrations
            btnCancel.Click += new EventHandler(btnCancel_click);
            btnSumbit.Click += new EventHandler(btnSubmitt_Click);
            #endregion

            if (Request.QueryString["EventAttendeeID"] != null)
            {
                strAttendeeID = Convert.ToInt32(Request.QueryString["EventAttendeeID"].ToString());

                if (!IsPostBack)
                {
                    int output = 1;
                    wgFeedbackDetails_1.DataSource = Feedback.CheckFeedbackStatus(strAttendeeID, ref output);
                    wgFeedbackDetails_1.DataBind();

                    if (wgFeedbackDetails_1.Rows.Count > 0)
                        wgFeedbackDetails_1.Visible = true;

                    if (output == 0) // Eligible for submit the Feedback
                    {
                        plDetails.Visible = true;

                        DataSet ds = Feedback.GetFeedBackFormDetails(strAttendeeID);

                        #region BindData
                        // using CCollection 

                        CCollection loColl = new CCollection(new CMTSubServiceLine(), true);
                        DataTable dtSSL = loColl.DataTable;
                        ddlSSL.DataSource = dtSSL;
                        ddlSSL.DataBind();
                        ddlSSL.Items.Insert(0, new ListItem("Select Sub ServiceLine", "0"));
                        ddlSSL.SelectedIndex = 0;

                        CCollection loColStage = new CCollection(new CMTStage(), true);
                        DataTable dtStage = loColStage.DataTable;
                        ddlStage.DataSource = dtStage;
                        ddlStage.DataBind();
                        ddlStage.Items.Insert(0, new ListItem("Select Stage", "0"));
                        ddlStage.SelectedIndex = 0;

                        CCollection loColProbablility = new CCollection(new CMTStage(), true);
                        DataTable dtProbablility = loColProbablility.DataTable;
                        ddlProbablility.DataSource = dtProbablility;
                        ddlProbablility.DataBind();
                        ddlProbablility.Items.Insert(0, new ListItem("Select Probablity of Win", "0"));
                        ddlProbablility.SelectedIndex = 0;

                        CCollection loColTimeFrame = new CCollection(new CMTTimeFrame(), true);
                        DataTable dtTimeFrame = loColTimeFrame.DataTable;
                        ddlTimeframe.DataSource = dtTimeFrame;
                        ddlTimeframe.DataBind();
                        ddlTimeframe.DataSource = dtTimeFrame;
                        ddlTimeframe.DataBind();
                        ddlTimeframe.Items.Insert(0, new ListItem("Select Time Frame", "0"));
                        ddlTimeframe.SelectedIndex = 0;

                        CMTCompany objCompany = new CMTCompany();
                        objCompany.EventAttendeeID = strAttendeeID.ToString();
                        CCollection loColCompany = new CCollection(objCompany, true);
                        DataTable dtCompaany = loColCompany.DataTable;

                        ddlCompanyName.DataSource = dtCompaany;
                        ddlCompanyName.DataBind();
                        ddlCompanyName.Items.Insert(0, new ListItem("Select Company", "0"));
                        ddlCompanyName.SelectedIndex = 0;

                        CMTCOuntry objCountry = new CMTCOuntry();
                        objCountry.EventAttendeeID = strAttendeeID.ToString();
                        CCollection loColCountry = new CCollection(objCountry, true);
                        DataTable dtCountry = loColCountry.DataTable;

                        ddlCountry.DataSource = dtCountry;
                        ddlCountry.DataBind();
                        ddlCountry.Items.Insert(0, new ListItem("Select Country", "0"));
                        ddlCountry.SelectedIndex = 0;
                        bindParticipants(ds.Tables[6]);
                        #endregion
                    }
                    else
                    {
                        plDetails.Visible = false;
                        lblLoadMessage.Text = "Access denied.";
                    }
                }
            }
            else
            {
                plDetails.Visible = false;
                lblLoadMessage.Text = "Access denied.";
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

    private void bindParticipants(DataTable dtParticipants)
    {
        ddlParticipant.DataSource = dtParticipants;
        ddlParticipant.DataBind();
        ddlParticipant.Items.Insert(0, new ListItem("Select Participant", "0"));
        ddlParticipant.SelectedIndex = 0;
    }
    #endregion

    #region btnSubmitt_Click
    /// <summary>
    /// Event to save the feedback entered.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmitt_Click(object sender, EventArgs e)
    {
        try
        {
            CMTFeedback objFeedback = new CMTFeedback();
            ListItemCollection Feedback = new ListItemCollection();
            Feedback.Clear();
            Feedback.Add(new ListItem(Feedbacks.Company.ToString(), ddlCompanyName.SelectedValue));
            Feedback.Add(new ListItem(Feedbacks.Host.ToString(), txtHost.Text.Trim()));
            Feedback.Add(new ListItem(Feedbacks.Tal.ToString(), txtTAL.Text.Trim()));
            Feedback.Add(new ListItem(Feedbacks.ITSSMP.ToString(), txtITSSMP.Text.Trim()));
            Feedback.Add(new ListItem(Feedbacks.ITSSMP.ToString(), txtITSSMP.Text.Trim()));
            Feedback.Add(new ListItem(Feedbacks.ClientAttendees.ToString(), txtClientAttendee.Text.Trim()));
            Feedback.Add(new ListItem(Feedbacks.CreatedEventAttendeeID.ToString(), strAttendeeID.ToString()));
            Feedback.Add(new ListItem(Feedbacks.EventAttendeeID.ToString(), ddlParticipant.SelectedValue.ToString()));
            Feedback.Add(new ListItem(Feedbacks.Country.ToString(), ddlCountry.SelectedValue.ToString()));
            Feedback.Add(new ListItem(Feedbacks.SubServiceLine.ToString(), ddlSSL.SelectedValue.ToString()));
            Feedback.Add(new ListItem(Feedbacks.PrimObjectivesChallenges.ToString(), txtbusinessobj.Text));
            Feedback.Add(new ListItem(Feedbacks.PrimTaxIssues.ToString(), txtTaxIssues.Text));

            if (rdblistOpportunityProject.Items[0].Selected == true)
                Feedback.Add(new ListItem(Feedbacks.OpportunityProjectIdentified.ToString(), rdblistOpportunityProject.Items[0].Text));
            else
                Feedback.Add(new ListItem(Feedbacks.OpportunityProjectIdentified.ToString(), rdblistOpportunityProject.Items[1].Text));

            Feedback.Add(new ListItem(Feedbacks.DescrOpportunityProject.ToString(), txtDescoftheOpportunity.Text));
            Feedback.Add(new ListItem(Feedbacks.Stage.ToString(), ddlStage.SelectedValue.ToString()));
            Feedback.Add(new ListItem(Feedbacks.PotentialTaxFees.ToString(), txtPotentialTaxFees.Text));
            Feedback.Add(new ListItem(Feedbacks.ProbabilityOfWin.ToString(), ddlProbablility.SelectedValue.ToString()));
            Feedback.Add(new ListItem(Feedbacks.ExpectedTimeframe.ToString(), ddlTimeframe.SelectedValue.ToString()));
            Feedback.Add(new ListItem(Feedbacks.NextSteps.ToString(), txtNextSteps.Text));
            Feedback.Add(new ListItem(Feedbacks.ResponsibilityTakeForward.ToString(), txtResponsibility.Text));
            Feedback.Add(new ListItem(Feedbacks.AdditionalInformation.ToString(), txtAdditonalInfo.Text));

            XDocument fieldadmins = new XDocument();
            XElement xRoot = new XElement(@"Feedback");
            fieldadmins.Add(xRoot);

            foreach (ListItem item in Feedback)
                xRoot.Add(new XElement(item.Text, item.Value));

            CMTFeedback objFeedBack = new CMTFeedback();
            objFeedback.FeedBackData = xRoot.ToString();
            CCollection loColFeedback = new CCollection(objFeedBack, true);


            string strOutput = objFeedback.AddFeedBAck(xRoot.ToString());
            lblMessage.Text = strOutput;

            if (!strOutput.Contains("Error"))
                lblMessage.ForeColor = Color.Green;
            else
                lblMessage.ForeColor = Color.Red;

            int output = 0;
            CMTFeedback feedback = new CMTFeedback();



            wgFeedbackDetails_1.DataSource = feedback.CheckFeedbackStatus(strAttendeeID, ref output);
            wgFeedbackDetails_1.DataBind();

            if (wgFeedbackDetails_1.Rows.Count > 0)
                wgFeedbackDetails_1.Visible = true;

            ClearAllFields();
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region wgFeedbackDetails_1_PageIndexChanged
    /// <summary>
    /// Event to change the page index of the feedback grid and rebind grid.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void wgFeedbackDetails_1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            wgFeedbackDetails_1.PageIndex = e.NewPageIndex;
            int output = 0;
            CMTFeedback feedback = new CMTFeedback();
            wgFeedbackDetails_1.DataSource = feedback.CheckFeedbackStatus(strAttendeeID, ref output);
            wgFeedbackDetails_1.DataBind();

            if (wgFeedbackDetails_1.Rows.Count > 0)
                wgFeedbackDetails_1.Visible = true;
        }
        catch (Exception)
        {
            throw;
        }
    }

    #endregion

    #region btnCancel_click
    /// <summary>
    /// Event to clear all the entered fields.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCancel_click(object sender, EventArgs e)
    {
        try
        {
            ClearAllFields();
        }
        catch (Exception)
        {
            throw;
        }
    }

    #region ClearAllFields
    /// <summary>
    /// Method to clear all the selected fields.
    /// </summary>
    /// <param name="clear"></param>
    private void ClearAllFields()
    {
        ddlCompanyName.SelectedIndex = 0;
        txtHost.Text = "";
        txtTAL.Text = "";
        txtITSSMP.Text = "";
        txtClientAttendee.Text = "";
        ddlParticipant.SelectedIndex = 0;
        ddlSSL.SelectedIndex = 0;
        ddlStage.SelectedIndex = 0;
        ddlTimeframe.SelectedIndex = 0;
        ddlProbablility.SelectedIndex = 0;
        ddlCountry.SelectedIndex = 0;
        txtbusinessobj.Text = "";
        txtTaxIssues.Text = "";
        txtResponsibility.Text = "";
        txtPotentialTaxFees.Text = "";
        txtNextSteps.Text = "";
        txtDescoftheOpportunity.Text = "";
        txtbusinessobj.Text = "";
        txtAdditonalInfo.Text = "";
        rdblistOpportunityProject.Items[0].Selected = false;
        rdblistOpportunityProject.Items[0].Selected = false;
    }
    #endregion

    #endregion

}


